﻿Public Class TGeneralSettings
    Implements System.IDisposable
    Private Const CONFIG_FILE As String = "generalsettings.conf"
    Private Const DEFAULT_LIMIT As Integer = 30

    Private _FontList As New TFontList
    Private _BackColor As Color
    Private _ForeColor As Color
    Private _FontName As String
    Private _FontSize As Integer
    Private _FontBold As Boolean
    Private _FontItalic As Boolean
    Private _Maximized As Boolean
    Private _TopPos As Integer
    Private _LeftPos As Integer
    Private _Height As Integer
    Private _Width As Integer
    Private _DefaultLimit As Integer

    Private _ShowData As Boolean
    Private _ShowAll As Boolean

    Public ReadOnly Property Font(ByVal Index As Integer) As FontFamily
        Get
            Return _FontList.Font(Index)
        End Get
    End Property

    Public ReadOnly Property FontCount() As Integer
        Get
            Return _FontList.Count
        End Get
    End Property

    Private Sub ApplyForeColor(ByVal AControl As Control)
        If AControl.Controls.Count > 0 Then
            Return
        ElseIf AControl.GetType IsNot GetType(Sgry.Azuki.WinForms.AzukiControl) Then
            AControl.ForeColor = _ForeColor
        Else
            Return
        End If
    End Sub

    Private Sub ApplyBackColor(ByVal AControl As Control)
        If AControl.Controls.Count > 0 Then
            Return
        ElseIf AControl.GetType IsNot GetType(Sgry.Azuki.WinForms.AzukiControl) AndAlso AControl.GetType IsNot GetType(ToolStrip) AndAlso AControl.GetType IsNot GetType(CheckBox) AndAlso AControl.GetType IsNot GetType(Label) Then
            AControl.BackColor = _BackColor
        Else
            Return
        End If
    End Sub

    Private Sub ApplyFont(ByVal AControl As Control)

        If AControl.GetType Is GetType(TabControl) Then
            AControl.Font = Me.Font
        ElseIf AControl.Controls.Count > 0 Then
            Return
        ElseIf AControl.GetType IsNot GetType(Sgry.Azuki.WinForms.AzukiControl) Then
            If Not (AControl.Tag IsNot Nothing AndAlso AControl.Tag.ToString = "NO") Then
                AControl.Font = Me.Font
            End If
        Else
            Return
        End If

    End Sub

    Public Sub Apply(ByVal AControl As Control)
        ApplyForeColor(AControl)
        ApplyBackColor(AControl)
        ApplyFont(AControl)
    End Sub

    Public Sub ApplyAll(ByVal AControl As Control)
        ApplyAllFonts(AControl)
        ApplyAllForeColors(AControl)
        ApplyAllBackColors(AControl)
    End Sub

    'AzukiControl→対象外
    'Label→BackColorは対象外
    'TabControl→子があるけどFontは対象
    Public Sub ApplyAllBackColors(ByVal AControl As Control)
        For Each ctrl As Control In AControl.Controls
            If ctrl.Controls.Count > 0 Then
                Call ApplyAllBackColors(ctrl)
            ElseIf ctrl.GetType IsNot GetType(Sgry.Azuki.WinForms.AzukiControl) AndAlso ctrl.GetType IsNot GetType(ToolStrip) AndAlso ctrl.GetType IsNot GetType(CheckBox) AndAlso ctrl.GetType IsNot GetType(Label) Then
                ctrl.BackColor = _BackColor
            End If
        Next
    End Sub

    Public Sub ApplyAllForeColors(ByVal AControl As Control)
        For Each ctrl As Control In AControl.Controls
            If ctrl.Controls.Count > 0 Then
                Call ApplyAllForeColors(ctrl)
            ElseIf ctrl.GetType IsNot GetType(Sgry.Azuki.WinForms.AzukiControl) AndAlso ctrl.GetType IsNot GetType(ToolStrip) Then
                ctrl.ForeColor = _ForeColor
            End If
        Next
    End Sub

    Public Sub ApplyAllFonts(ByVal AControl As Control)
        Dim F As Font = Me.Font
        For Each ctrl As Control In AControl.Controls
            If ctrl.GetType Is GetType(TabControl) Then
                ctrl.Font = F
            End If
            If ctrl.Controls.Count > 0 Then
                Call ApplyAllFonts(ctrl)
            ElseIf ctrl.GetType IsNot GetType(Sgry.Azuki.WinForms.AzukiControl) Then
                If Not (ctrl.Tag IsNot Nothing AndAlso ctrl.Tag.ToString = "NO") Then
                    ctrl.Font = F
                End If
            End If
        Next
    End Sub

    Private ReadOnly Property ConfigFile() As String
        Get
            Return Common.ApplicationPath & CONFIG_FILE
        End Get
    End Property

    Public Property ShowAll() As Boolean
        Get
            Return _ShowAll
        End Get
        Set(ByVal value As Boolean)
            _ShowAll = value
        End Set
    End Property
    Public Property ShowData() As Boolean
        Get
            Return _ShowData
        End Get
        Set(ByVal value As Boolean)
            _ShowData = value
        End Set
    End Property

    Public Sub SetDefaultLimit(ByVal AValueString As String)
        _DefaultLimit = Common.StrToIntDef(AValueString, DEFAULT_LIMIT)
    End Sub

    Public Property DefaultLimit() As Integer
        Get
            Return _DefaultLimit
        End Get
        Set(ByVal value As Integer)
            _DefaultLimit = value
        End Set
    End Property

    Public Property Font() As Font
        Get
            Dim fs As FontStyle = FontStyle.Regular
            If _FontBold Then
                fs = fs Or FontStyle.Bold
            End If
            If _FontItalic Then
                fs = fs Or FontStyle.Italic
            End If
            Return New Font(_FontName, _FontSize, fs)
        End Get
        Set(ByVal value As Font)
            _FontBold = value.Bold
            _FontItalic = value.Italic
            _FontSize = value.Size
            _FontName = value.Name
        End Set
    End Property

    Public Sub New()
        Refresh()
    End Sub

    Public Sub SaveConfig()
        Dim IniFile As New TIniFile(Me.ConfigFile)
        Try
            IniFile.WriteInteger("Table", "DefaultLimit", _DefaultLimit)
            IniFile.WriteBool("Table", "ShowData", _ShowData)
            IniFile.WriteBool("Table", "ShowAll", _ShowAll)
            IniFile.WriteColor("General", "BackColor", _BackColor)
            IniFile.WriteColor("General", "ForeColor", _ForeColor)
            IniFile.WriteString("Font", "Name", _FontName)
            IniFile.WriteInteger("Font", "Size", _FontSize)
            IniFile.WriteBool("Font", "Bold", _FontBold)
            IniFile.WriteBool("Font", "Italic", _FontItalic)
            IniFile.WriteBool("Position", "Maximized", _Maximized)
            IniFile.WriteInteger("Position", "Height", _Height)
            IniFile.WriteInteger("Position", "Width", _Width)
            IniFile.WriteInteger("Position", "Top", _TopPos)
            IniFile.WriteInteger("Position", "Left", _LeftPos)
        Finally
            IniFile.Dispose()
        End Try
    End Sub

    Public Sub Refresh()
        Dim IniFile As New TIniFile(Me.ConfigFile)
        Try
            _DefaultLimit = IniFile.ReadInteger("Table", "DefaultLimit", DEFAULT_LIMIT)
            _ShowData = IniFile.ReadBool("Table", "ShowData", False)
            _ShowAll = IniFile.ReadBool("Table", "ShowAll", False)

            _BackColor = IniFile.ReadColor("General", "BackColor", Color.White)
            _ForeColor = IniFile.ReadColor("General", "ForeColor", Color.Black)
            _FontName = IniFile.ReadString("Font", "Name", "MS UI Gothic")
            _FontSize = IniFile.ReadInteger("Font", "Size", 9)
            _FontBold = IniFile.ReadBool("Font", "Bold", False)
            _FontItalic = IniFile.ReadBool("Font", "Italic", False)
            _Maximized = IniFile.ReadBool("Position", "Maximized", True)
            _Height = IniFile.ReadInteger("Position", "Height", 600)
            _Width = IniFile.ReadInteger("Position", "Width", 900)
            _TopPos = IniFile.ReadInteger("Position", "Top", Convert.ToInt32(System.Math.Truncate((Screen.PrimaryScreen.WorkingArea.Height - Me.Height) / 2)))
            _LeftPos = IniFile.ReadInteger("Position", "Left", Convert.ToInt32(System.Math.Truncate((Screen.PrimaryScreen.WorkingArea.Width - Me.Width) / 2)))
        Finally
            IniFile.Dispose()
        End Try
    End Sub

    Public Property BackColor() As Color
        Get
            Return _BackColor
        End Get
        Set(ByVal value As Color)
            _BackColor = value
        End Set
    End Property

    Public Property ForeColor() As Color
        Get
            Return _ForeColor
        End Get
        Set(ByVal value As Color)
            _ForeColor = value
        End Set
    End Property
    Public Property FontName() As String
        Get
            Return _FontName
        End Get
        Set(ByVal value As String)
            _FontName = value
        End Set
    End Property
    Public Property FontSize() As Integer
        Get
            Return _FontSize
        End Get
        Set(ByVal value As Integer)
            _FontSize = value
        End Set
    End Property
    Public Property FontBold() As Boolean
        Get
            Return _FontBold
        End Get
        Set(ByVal value As Boolean)
            _FontBold = value
        End Set
    End Property
    Public Property FontItalic() As Boolean
        Get
            Return _FontItalic
        End Get
        Set(ByVal value As Boolean)
            _FontItalic = value
        End Set
    End Property
    Public Property Maximized() As Boolean
        Get
            Return _Maximized
        End Get
        Set(ByVal value As Boolean)
            _Maximized = value
        End Set
    End Property
    Public Property TopPos() As Integer
        Get
            Return _TopPos
        End Get
        Set(ByVal value As Integer)
            _TopPos = value
        End Set
    End Property
    Public Property LeftPos() As Integer
        Get
            Return _LeftPos
        End Get
        Set(ByVal value As Integer)
            _LeftPos = value
        End Set
    End Property
    Public Property Height() As Integer
        Get
            Return _Height
        End Get
        Set(ByVal value As Integer)
            _Height = value
        End Set
    End Property
    Public Property Width() As Integer
        Get
            Return _Width
        End Get
        Set(ByVal value As Integer)
            _Width = value
        End Set
    End Property

    Private disposedValue As Boolean = False        ' 重複する呼び出しを検出するには

    ' IDisposable
    Protected Overridable Sub Dispose(ByVal disposing As Boolean)
        If Not Me.disposedValue Then
            If disposing Then
                ' TODO: 他の状態を解放します (マネージ オブジェクト)。
            End If
            ' TODO: ユーザー独自の状態を解放します (アンマネージ オブジェクト)。
            ' TODO: 大きなフィールドを null に設定します。
        End If
        Me.disposedValue = True
    End Sub

#Region " IDisposable Support "
    ' このコードは、破棄可能なパターンを正しく実装できるように Visual Basic によって追加されました。
    Public Sub Dispose() Implements IDisposable.Dispose
        ' このコードを変更しないでください。クリーンアップ コードを上の Dispose(ByVal disposing As Boolean) に記述します。
        Dispose(True)
        GC.SuppressFinalize(Me)
    End Sub
#End Region

End Class
